package com.select.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.select.course.entity.SchoolCollege;
import com.select.course.mapper.SchoolCollegeMapper;
import com.select.course.service.SchoolCollegeService;
import com.select.course.utils.PageUtils;
import com.select.course.vo.AllVo;
import com.select.course.vo.CollegeVo;
import com.select.course.vo.PageVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 24820
* @description 针对表【c_school_college】的数据库操作Service实现
* @createDate 2022-10-26 23:08:18
*/
@Service
public class SchoolCollegeServiceImpl extends ServiceImpl<SchoolCollegeMapper, SchoolCollege>
    implements SchoolCollegeService{



    @Override
    public List<CollegeVo> getCollegeGroup() {
        List<SchoolCollege> list = this.list();
        List<CollegeVo> collect = list.stream().map(e -> {
            CollegeVo collegeVo = new CollegeVo();
            BeanUtils.copyProperties(e, collegeVo);
            return collegeVo;
        }).collect(Collectors.toList());

        return collect;
    }
    @Override
    public PageUtils getAllCollege(PageVo pageVo) {
        Page<SchoolCollege> schoolCollegePage = new Page<SchoolCollege>(pageVo.getPageNum(), pageVo.getPageSize());
        String query = pageVo.getQuery();
        QueryWrapper<SchoolCollege> schoolCollegeQueryWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(query)) {
            schoolCollegeQueryWrapper.like("id",query).or().like("name",query);
        }
        Page<SchoolCollege> page = this.page(schoolCollegePage, schoolCollegeQueryWrapper);
        List<SchoolCollege> records = page.getRecords();
        List<AllVo> collect = records.stream().map(e -> {
            AllVo allVo = new AllVo();
            allVo.setCollege(e.getName());
            allVo.setRegisterTime(e.getRegisterTime());
            allVo.setId(e.getId());
            allVo.setDescript(e.getDescript());
            return allVo;
        }).collect(Collectors.toList());
        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(collect);
        return pageUtils;
    }

    @Override
    public void addCollege(AllVo allVo) {
        SchoolCollege schoolCollege = new SchoolCollege();
        schoolCollege.setName(allVo.getCollege());
        schoolCollege.setRegisterTime(new Date());
        schoolCollege.setDescript(allVo.getDescript());
        this.save(schoolCollege);
    }

    @Override
    public void deleteCollege(Long id) {
        this.removeById(id);
    }

    @Override
    public CollegeVo getCollegeInfo(Long collegeid) {
        CollegeVo collegeVo = new CollegeVo();

        SchoolCollege byId = this.getById(collegeid);
        collegeVo.setName(byId.getName());
        collegeVo.setDescript(byId.getDescript());
        collegeVo.setRegisterTime(byId.getRegisterTime());
        collegeVo.setId(byId.getId());


        return collegeVo;
    }

    @Override
    public void updateCollege(AllVo allVo) {
        SchoolCollege schoolCollege = new SchoolCollege();
        schoolCollege.setId(allVo.getId());
        schoolCollege.setName(allVo.getCollege());
        schoolCollege.setDescript(allVo.getDescript());
        this.updateById(schoolCollege);
    }

}




